package org.shushan.cloud.transaction.sample.service;


import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import org.shushan.cloud.transaction.sample.dao.UserMapper;
import org.shushan.cloud.transaction.sample.entity.UserEntity;
import org.shushan.cloud.transaction.sample.vo.UserPageRequest;
import org.shushan.cloud.transaction.sample.vo.UserRequest;
import org.shushan.cloud.transaction.sample.vo.UserResponse;
import org.shushan.framework.core.logger.ExceptionLog;
import org.shushan.framework.lock.annotation.DistributedLock;
import org.shushan.framework.mybatis.service.MybatisPlusServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;
import java.util.function.Function;

/**
 * @author zhigang.li
 * @date 2022/6/19
 
 
 */
@Primary
@Service("userServiceImpl")
public class UserServiceImpl extends MybatisPlusServiceImpl<UserPageRequest, UserRequest, UserResponse, String, UserEntity, UserMapper> implements UserService {

    @Override
    @Cacheable(key = "#id")
    public UserEntity getById(Serializable id) {
        return super.getById(id);
    }

    @Autowired
    UserMapper userMapper;

    @Override
    //@GlobalTransactional
    public boolean save(UserEntity entity) {
        boolean save = super.save(entity);
        //让它报错,测试事务回滚
        //int i = 10 / 0;
        return save;
    }

    public static void main(String[] args) {
        System.out.println("FD006868686800000000000000000825280418B4AAC2798A9E280000".length());
    }

    @Override
    @ExceptionLog
    public String test(String name, String id) {
        //测试异常打印方法参数,注解在接口上不生效
        System.out.println(4 / 0);
        return null;
    }

    @Override
    @DistributedLock
    public String lock(int i) throws InterruptedException {
        System.out.println(i);
        Thread.sleep(3000);
        return "lock" + i;
    }

    @Override
    //@GlobalTransactional
    public void saveTransaction(UserRequest request) {
        userMapper.insert(toEntity(request, UserEntity.class));
        int i = 10 / 0;
    }

    @Override
    public LambdaQueryChainWrapper<UserEntity> getLambdaQueryForList(UserPageRequest request) {
        //设置用户的page的查询条件
        return lambdaQuery().likeRight(UserEntity::getUsername, request.getUsername());
    }

    @Override
    public LambdaQueryChainWrapper<UserEntity> getLambdaQueryForPage(UserPageRequest request) {
        //分页结果查询条件
        return super.getLambdaQueryForPage(request);
    }


    @Override
    public Function<List<UserEntity>, List<UserResponse>> toResultListFunction(Class<UserResponse> dest) {
        //自定义转换成结果集
        return super.toResultListFunction(dest);
    }

    @Override
    public Function<List<UserRequest>, List<UserEntity>> toEntityListFunction(Class<UserEntity> dest) {
        //自定义转成结果集
        return super.toEntityListFunction(dest);
    }
}
